<?php
/**
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 3 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/

/**
 * String manipulations.
 * @package Util
 * @author nullstring
 */
namespace Util;

class StringHandler
{
	/**
	 * Constructor protecte the classs, because it is stataic.
	 * @access private
	 * @throws Exception
	 * @return void
	 */
	private function __construct()
	{
		throw new \Exception("Illegal operation, this class is only static class!");
	}

	/**
	 * Convert non locale characters to [a-z] characters
	 * TODO : needs to be expanded with all kind of letters
	 * @access public
	 * @static
	 * @param  string $string
	 * @return string
	 */
	public static function localeToCommonLower($string, $charset = null)
	{
		if ($charset === null) {
			$charset = CHARSET;
		}

		$string     = mb_strtolower($string, $charset);
		$chars_from = array("ö", "ü", "ó", "ő", "ú", "é", "á", "ű", "í", "û", "õ", "ä", "ß");
		$chars_to   = array("o", "u", "o", "o", "u", "e", "a", "u", "i", "u", "o", "a", "ss");
		$string     = str_replace($chars_from, $chars_to, $string);

		return $string;
	}


	/**
	 * Remove special characters from the string.
	 * @access public
	 * @static
	 * @param  string $string
	 * @return string
	 */
	public static function createAlias($string, $charset = null)
	{
		if ($charset === null) {
			$charset = CHARSET;
		}

		$string     = trim($string);
		$string     = static::localeToCommonLower($string, $charset);
		$string     = preg_replace("/[^a-z0-9-]/i", "-", $string);

		do {
			$string = str_replace("--", "-", $string);
		} while (ereg("--", $string));

		$string = trim($string, "-");

		return $string;
	}

	/**
	 * Create safe filname without special chars etc.
	 * @access public
	 * @static
	 * @param  string $fileName
	 * @return string
	 */
	public static function safeFileName($fileName, $charset = null)
	{
		if ($charset === null) {
			$charset = CHARSET;
		}

		$ext  = static::fileExtension($fileName, false, false, $charset);
		$name = static::fileNameWithoutExtension($fileName, false, $charset);

		return rtrim(static::CreateAlias($name) . "." .  static::CreateAlias($ext), ".");
	}

	/**
	 * Returns with file extension.
	 * @access public
	 * @static
	 * @param string $fileName
	 * @param boolean $converToLowerCase
	 * @param boolean $startingWithDot
	 * @param string $charset
	 * @return string
	 */
	public static function fileExtension($fileName, $converToLowerCase = false, $startingWithDot = false , $charset = null)
	{
		if ($charset === null) {
			$charset = CHARSET;
		}
		
		$extWithDot = strrchr(ltrim($fileName, "."), ".");
		$extWithDot = ($converToLowerCase === false) ? $extWithDot : mb_strtolower($extWithDot, $charset) ;
		$extWithDot = ($extWithDot)? $extWithDot : "." ;

		return ($startingWithDot)? $extWithDot : ltrim($extWithDot, ".");
	}

	/**
	 * Returns the file name without extension.
	 * @access public
	 * @static
	 * @param string $fileName
	 * @param boolean $converToLowerCase
	 * @param string $charset
	 * @return string
	 */
	public static function fileNameWithoutExtension($fileName, $converToLowerCase = false, $charset = null)
	{
		if ($charset === null) {
			$charset = CHARSET;
		}

		$extension = static::fileExtension($fileName);
		$file = ($extension === "") ? $fileName : substr($fileName, 0, -strlen($extension));
		$file = rtrim($file, ".");
		$file = ($converToLowerCase === false) ? $file : mb_strtolower($file, $charset) ;
		
		return $file;
	}


	/**
	 * Convert any localization string's first letter to uppercase.
	 * @access public
	 * @static
	 * @param  string $string
	 * @return string
	 */
	public static function ucFirst($string, $encoding = null)
	{
		if ($charset === null) {
			$charset = CHARSET;
		}

		$first    = mb_strtoupper(mb_substr($string, 0, 1, $encoding), $encoding);
		$string   = $first . mb_substr($string, 1, mb_strlen($string, $encoding), $encoding);
		
		return $string;
	}

	/**
	 * Creates an email anchor.
	 *
	 * @access public
	 * @static
	 * @param   string  $email
	 * @param   string  $title
	 * @return  string
	 */
	public static function mailto($email, $title = null)
	{
		if (strpos($email, "?") !== false) {
			list($email, $params) = explode("?", $email, 2);

			$params = "?" . str_replace(" ", "%20", $params);
		} else {
			$params = "";
		}

		$safe = static::createEmail($email);

		if (is_null($title)) {
			$title = $safe;
		}

		return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;' . $safe . $params . '">' . $title . '</a>';
	}

	/**
	 * Generates an obfuscated version of an email address.
	 * @access public
	 * @static
	 * @param   string  $email
	 * @return  string
	 */
	public static function createEmail($email)
	{
		$safe = "";
		
		foreach (str_split($email) as $letter) {
			switch (($letter === "@") ? rand(1, 2) : rand(1, 3)) {
				case 1: $safe .= "&#" . ord($letter) . ";";
				break;
				case 2: $safe .= "&#x" . dechex(ord($letter)) . ";";
				break;
				case 3: $safe .= $letter;
			}
		}
		
		return $safe;
	}

	/**
	 * Makes a query's where part from the parameters.
	 * @access public
	 * @static
	 * @param array $keys
	 * @param array $tableAndFields associative array: array( "table" => array("searchField1", "searchField2") )
	 * @return string
	 */
	public static function search($keys, $tableAndFields)
	{
		foreach ($table as $key=>$value) {
			$where = "";
			foreach ($value as $index=>$field) {
				if ($index > 0) {
					$where .= ") OR (";
				} else {
					$where .= "(";
				}
				foreach ($keys as $kindex=>$keyword) {
					if ($kindex > 0) {
						$where .= " AND ";
					}
					$where .= "`" . $key . "`.`" . $field . "` LIKE '%" . $keyword . "%'";
				}
			}
			$where .= ")";
		}
		
		return $where;
	}

	/**
	 * Creates an urlencoded input, except slash ( / ), percent ( % ) wont be encoded, cause the framework don't accep urlencoded (/, %) signs.
	 * @access public
	 * @static
	 * @param string $string
	 * @return string
	 */
	public static function encodeSearchURL($string)
	{
		$string = urlencode($string);
		$string = str_replace("%2F", "/", $string);
		$string = str_replace("%25", "+", $string);
		
		return $string;
	}

	/**
	 * Creates an urldencoded input, except slash ( / ), percent ( % ) wont be decodes, cause the framework don't accep urlencoded (/, %) signs.
	 * @access public
	 * @static
	 * @param string $string
	 * @return string
	 */
	public static function decodeSearchURL($string )
	{
		$string = str_replace("/", "%2F", $string);
		$string = str_replace("+", "%25", $string);
		$string = urldecode($string);
		
		return $string;
	}
}

?>